package org.cfd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Collections;
import org.cfd.entity.Addresses;
import org.cfd.service.AddressesService;
import org.cfd.utils.Code;
import org.cfd.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * AddressesController 是用于处理针对地址对象的请求的控制器。 它提供了创建、获取、更新、删除和列举地址的API接口。
 *
 * @author rjs
 */
@RestController
@CrossOrigin
@RequestMapping("/Address")
public class AddressesController {

  private final AddressesService addressesService;

  /**
   * 使用Autowired注解进行依赖注入，注入AddressesService服务类。
   *
   * @param addressesService 地址服务
   */
  @Autowired
  public AddressesController(AddressesService addressesService) {
    this.addressesService = addressesService;
  }

  /**
   * 创建新地址的API接口。
   *
   * @param addresses 地址对象
   * @return 返回保存后的地址对象
   */
  @PostMapping
  public Result createAddress(@RequestBody Addresses addresses) {
    addressesService.save(addresses);
    return new Result(Code.ADD_SUCCESS, "添加成功");
  }

  /**
   * 获取特定地址的API接口。
   *
   * @param id 地址ID
   * @return 返回指定ID的地址对象
   */
  @GetMapping("/{id}")
  public Result getAddress(@PathVariable("id") Integer id) {
    Addresses addresses = addressesService.getById(id);
    if (addresses != null) {
      return new Result(Code.SELECT_SUCCESS, "获取成功", addresses);
    } else {
      return new Result(Code.SELECT_ERROR, "获取失败");
    }
  }

  /**
   * 更新特定地址的API接口。
   *
   * @param id 地址ID
   * @param newAddresses 新的地址对象
   * @return 返回更新后的地址对象
   */
  @PutMapping("/{id}")
  public Result updateAddress(@PathVariable("id") Integer id, @RequestBody Addresses newAddresses) {
    Addresses existingAddresses = addressesService.getById(id);
    if (existingAddresses != null) {
      existingAddresses.setUserId(newAddresses.getUserId());
      existingAddresses.setName(newAddresses.getName());
      existingAddresses.setPhone(newAddresses.getPhone());
      existingAddresses.setAddress(newAddresses.getAddress());
      addressesService.updateById(existingAddresses);
      return new Result(Code.UPDATE_SUCCESS, "更新成功");
    } else {
      return new Result(Code.UPDATE_ERROR, "更新失败");
    }
  }

  /**
   * 删除特定地址的API接口。
   *
   * @param id 地址ID
   * @return 如果地址被成功删除，返回200 OK状态；否则返回404 Not Found状态。
   */
  @DeleteMapping("/{id}")
  public Result deleteAddress(@PathVariable("id") Integer id) {
    if (addressesService.removeById(id)) {
      return new Result(Code.DELETE_SUCCESS, "删除成功");
    } else {
      return new Result(Code.DELETE_ERROR, "删除失败");
    }
  }

  /**
   * 获取地址列表的API接口，支持分页查询。
   *
   * @param page 要获取的页数
   * @param size 每页的记录数
   * @return 返回地址列表的分页对象
   */
  @GetMapping("/page")
  public Result listAddresses(
      @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
    Page<Addresses> addressesPage = new Page<>(page, size);
    addressesService.page(addressesPage);
    if (addressesPage.getRecords().isEmpty()) {
      return new Result(Code.SELECT_ERROR, "获取失败", Collections.emptyList());
    }
    return new Result(Code.SELECT_SUCCESS, "获取成功", addressesPage);
  }
}
